/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.view.commons;

import java.util.ArrayList;  
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.validator.GenericValidator;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IWorkbenchPartSite;
import org.mags.common.EngineContext;
import org.mags.remas.control.EntityEditorInput;
import org.mags.remas.control.EntityRelationshipController;
import org.mags.remas.control.ListEditorInput;
import org.mags.remas.control.SubentitiesEditorInput;
import org.mags.remas.control.commons.ControlUtils;
import org.mags.remas.control.commons.CrudAction;
import org.mags.remas.control.commons.IAction;
import org.mags.remas.control.commons.IStartClass;
import org.mags.remas.exception.SingletonExceptionHandler;
import org.mags.remas.logic.EntityLogic;
import org.mags.remas.model.entity.CommentType;
import org.mags.remas.model.entity.Entity;
import org.mags.remas.model.entity.EntityCategory;
import org.mags.remas.model.entity.EntityRelationship;
import org.mags.remas.model.entity.EntityStructure;
import org.mags.remas.model.entity.EntityType;
import org.mags.remas.view.CommentsEditor;
import org.mags.remas.view.EntityEditor;
import org.mags.remas.view.EntityView;
import org.mags.remas.view.PropertiesView;
import org.mags.remas.view.ReferencesFromEditor;
import org.mags.remas.view.ReferencesToEditor;
import org.mags.remas.view.SubentitiesEditor;
import org.mags.remas.view.dialogs.EditablePopupDialog;
import org.mags.remas.view.dialogs.RelationshipTreeDialog;
import org.mags.remas.view.dialogs.SingletonRelationshipTreeDialog;
import org.mags.remas.view.widgets.RelationshipTreeViewer;
import org.mags.remas.model.entity.EntityComment;

/**
 * 
 * @author Mario Gaitan
 *
 */
public class TreeActionMaker {
	final private RelationshipTreeViewer treeViewer;
	final private EntityRelationshipController viewController;
	final private IWorkbenchPartSite site;
	private Menu menuRightClick;
	private Menu menuDrop;
	private Action deleteAction;
	private Action editAction;	
	private Action refreshAction;
	private Action moveAction;
	private Action copyAction;
	private Action moveAboveAction;
	private Action moveBelowAction;
	private Action previewAction;
	private Action processAction;
	private Action editFileAction;
	private Action subentitiesEditAction;
	private Action propertiesAction;
	private Action commentsAction;
	private Action addCommentAction;
	private IActionMenuMaker actionMenuMaker;
	private Action referencesFromAction;
	private Action referenceFromAction;
	private Action referenceToAction;
	private Action referencesToAction;
	private Action selectInViewAction;
	private Action selectLinkInViewAction;
	
	public Action getSelectLinkInViewAction() {
		return selectLinkInViewAction;
	}
	public Action getSelectInViewAction() {
		return selectInViewAction;
	}
	public Action getReferencesToAction() {
		return referencesToAction;
	}
	public Action getReferenceToAction() {
		return referenceToAction;
	}
	public Action getReferenceFromAction() {
		return referenceFromAction;
	}
	public IActionMenuMaker getActionMenuMaker() {
		return actionMenuMaker;
	}
	public void setActionMenuMaker(IActionMenuMaker actionMenuMaker) {
		this.actionMenuMaker = actionMenuMaker;
	}
	public Menu getMenuRightClick() {
		return menuRightClick;
	}
	public Menu getMenuDrop() {
		return menuDrop;
	}
	public Action getAddCommentAction() {
		return addCommentAction;
	}
	public Action getPropertiesAction() {
		return propertiesAction;
	}
	public Action getEditFileAction() {
		return editFileAction;
	}
	public Action getProcessAction() {
		return processAction;
	}
	public Action getPreviewAction() {
		return previewAction;
	}
	public Action getDeleteAction() {
		return deleteAction;
	}
	public Action getEditAction() {
		return editAction;
	}
	public Action getRefreshAction() {
		return refreshAction;
	}
	public Action getMoveAction() {
		return moveAction;
	}
	public Action getCopyAction() {
		return copyAction;
	}
	public Action getMoveAboveAction() {
		return moveAboveAction;
	}
	public Action getMoveBelowAction() {
		return moveBelowAction;
	}
	public Action getSubentitiesEditAction() {
		return subentitiesEditAction;
	}
	public Action getCommentsAction() {
		return commentsAction;
	}
	public Action getReferencesFromAction() {
		return referencesFromAction;
	}
	public TreeActionMaker(RelationshipTreeViewer treeViewer
			, EntityRelationshipController viewController
			, IWorkbenchPartSite site) {
		if(treeViewer==null)
			throw new RuntimeException("treeViewer can't be null");
		if(viewController==null)
			throw new RuntimeException("viewController can't be null");
		if(site==null)
			throw new RuntimeException("site can't be null");
		this.treeViewer = treeViewer;
		this.viewController = viewController;
		this.site = site;
	}
	public void makeMenu() {
		MenuManager menuMgr = new MenuManager("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				if(actionMenuMaker!=null)
					actionMenuMaker.makeMenuForDrop(manager);
			}
		});
		menuDrop = menuMgr.createContextMenu(treeViewer.getControl());
		site.registerContextMenu(menuMgr, treeViewer);
		menuMgr = new MenuManager("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				if(actionMenuMaker!=null)
					actionMenuMaker.makeMenuForRightClick(manager);
			}
		});
		menuRightClick = menuMgr.createContextMenu(treeViewer.getControl());
		treeViewer.getControl().setMenu(menuRightClick);
		site.registerContextMenu(menuMgr, treeViewer);		
	}
	public List<Action> makeDinamicallyActions() {
		List<Action> actions = new ArrayList<Action>();
		final EntityRelationship entityRelationship = treeViewer.getSelectedElement();
		Entity entity = entityRelationship.getEntity();
		List<EntityStructure> allowedTypes = EntityLogic.getInstance().getEntityTypeChildren(entity.getEntityType());
		List<EntityStructure> loadedTypes = new ArrayList<EntityStructure>();
		boolean thereIsOne=false;
		Action action=null;
		Action subentitiesAction = null;
		if(allowedTypes!=null)
			for(EntityStructure i: allowedTypes) {
				if(loadedTypes.indexOf(i)<0) {
					final EntityType entityType = i.getEntityType();
					if(i.getViewType().getViewTypeId()==treeViewer.getViewType().getViewTypeId()) {
						loadedTypes.add(i);
						action = new Action() {
							public void run() 
							{ 
								try {
									final EntityRelationship newEntityRelationship = new EntityRelationship();
									newEntityRelationship.setEntity(new Entity());
									newEntityRelationship.getEntity().setEntityType(entityType);
									newEntityRelationship.setEntityParent(entityRelationship.getEntity());									
									EntityStructure entityStructure = EngineContext.getDataContext().getEntityProviderFactory().getEntityStructureProvider().getById(entityType.getEntityTypeId(), entityRelationship.getEntity().getEntityType().getEntityTypeId());
									boolean showEntityEditor = false;
									if(entityStructure.getLinkStructures().size()>0) {
										SingletonRelationshipTreeDialog treeDialog = SingletonRelationshipTreeDialog.getInstance(site.getShell().getDisplay());
										EntityCategory entityCategory = ControlUtils.selectCategory(entityStructure);
										if(entityCategory!=null)
											treeDialog.setSelectedEntityCategory(entityCategory);
										String text = ControlUtils.makeSelectLabel(entityStructure);
										text = "select: "+text;
										treeDialog.setText(text);					
										treeDialog.show();
										if(treeDialog.getStatus()==RelationshipTreeDialog.STATUS_CANCELED)
											return ;
										EntityRelationship selected = treeDialog.getSelectedEntityRelationship();
										if(entityStructure.getNullLink().equalsIgnoreCase("N")&&selected==null) {
											SingletonExceptionHandler.getInstance().showError("link can't be null");
											return ;
										}
										newEntityRelationship.setLink(selected);
										if(EngineContext.getDataContext().getEntityProviderFactory().getViewTypeProvider().isEDITVIEW(treeViewer.getViewType()))  {
											newEntityRelationship.getEntity().setName(newEntityRelationship.getEntity().getEntityType().getName());
											Map<String, Object> changes = viewController.create(newEntityRelationship, entityRelationship, null);
											EntityRelationship newRelationship = (EntityRelationship)changes.get("entityRelationship");
											treeViewer.setSelectedColumn(1);
											treeViewer.editElement(newRelationship, 1);
										}
										if(!EngineContext.getDataContext().getEntityProviderFactory().getViewTypeProvider().isEDITVIEW(treeViewer.getViewType()))  
											showEntityEditor = true;
									} else {
										if(EngineContext.getDataContext().getEntityProviderFactory().getViewTypeProvider().isEDITVIEW(treeViewer.getViewType()))  {
											newEntityRelationship.getEntity().setName(newEntityRelationship.getEntity().getEntityType().getName());
											Map<String, Object> changes = viewController.create(newEntityRelationship, entityRelationship, null);
											EntityRelationship newRelationship = (EntityRelationship)changes.get("entityRelationship");
											treeViewer.setSelectedColumn(1);
											treeViewer.editElement(newRelationship, 1);											
										} else
											showEntityEditor = true;
									}
									if(showEntityEditor) {
										EntityEditorInput entityEditorInput = new EntityEditorInput(newEntityRelationship);
										entityEditorInput.setParentEntityRelationship(entityRelationship);
										entityEditorInput.setAction(CrudAction.CREATE);
										EntityEditor entityEditor= (EntityEditor)site.getPage().openEditor(entityEditorInput, EntityEditor.ID);
										entityEditor.setViewController(viewController);
									}
								} catch(Exception e) {
									exceptionCatched(e);
								}
							}
						};
						action.setText("new "+entityType.getName());
						action.setToolTipText("add new element");
						ImageDescriptor imageDescriptor = ImageProvider.getEntityImageDescriptor(entityType.getCode().toLowerCase()+".png");
						if(imageDescriptor!=null)
							action.setImageDescriptor(imageDescriptor);
						actions.add(action);
					}
					if(EngineContext.getDataContext().getEntityProviderFactory().getViewTypeProvider().isEDITVIEW(i.getViewType())
					&& EngineContext.getDataContext().getEntityProviderFactory().getViewTypeProvider().isTREEVIEW(treeViewer.getViewType())) {
						if(!thereIsOne) {
							subentitiesAction = new Action() {
								public void run() {
									try {
										SubentitiesEditorInput subentitiesEditorInput = new SubentitiesEditorInput(entityRelationship);
										ViewUtils.openOrBringToTopEditor(subentitiesEditorInput, SubentitiesEditor.ID, site);
									} catch(Exception e) {
										exceptionCatched(e);
									}
								}
							};
							subentitiesAction.setText("show editor for "+entityRelationship.getEntity().getEntityType().getName());
							subentitiesAction.setToolTipText("show editor for this element");
							ImageDescriptor imageDescriptor = ImageProvider.getEntityImageDescriptor(entityRelationship.getEntity().getEntityType().getCode().toLowerCase()+".png");
							if(imageDescriptor!=null)
								subentitiesAction.setImageDescriptor(imageDescriptor);
							thereIsOne = true;
						}
					}
				}
			}
		if(subentitiesAction!=null)
			actions.add(subentitiesAction);	
		return actions;
	}
	public void makeActions() {
		moveAboveAction = new Action() {
			public void run() {
				try {
					viewController.moveAbove(treeViewer.getElementsDroped(), treeViewer.getTargetToDrop());
				} catch(Exception e) {
					exceptionCatched(e);
				}
			}
		};
		moveAboveAction.setText("move above");
		moveAboveAction.setToolTipText("move above selected elements");
		
		moveBelowAction = new Action() {
			public void run() {
				try {
					viewController.moveBelow(treeViewer.getElementsDroped(), treeViewer.getTargetToDrop());
				} catch(Exception e) {
					exceptionCatched(e);
				}
			}
		};
		moveBelowAction.setText("move below");
		moveBelowAction.setToolTipText("move below the selected elements");
		
		copyAction = new Action() {
			public void run() {
				try {
					viewController.copy(treeViewer.getElementsDroped(), treeViewer.getTargetToDrop());
				} catch(Exception e) {
					exceptionCatched(e);
				}
			}
		};
		copyAction.setText("copy elements");
		copyAction.setToolTipText("copy selected elements");
		
		moveAction = new Action() {
			public void run() {
				try {
					viewController.move(treeViewer.getElementsDroped(), treeViewer.getTargetToDrop());
				} catch(Exception e) {
					exceptionCatched(e);
				}
			}
		};
		moveAction.setText("move elements");
		moveAction.setToolTipText("move selected elements");
		
		referenceFromAction = new Action() {
			public void run() {
				try {
					viewController.referenceFrom(treeViewer.getElementsDroped(), treeViewer.getTargetToDrop());
				} catch(Exception e) {
					exceptionCatched(e);
				}
			}			
		};
		referenceFromAction.setText("reference elements from");
		referenceFromAction.setToolTipText("reference selected elements from");

		referenceToAction = new Action() {
			public void run() {
				try {
					viewController.referenceTo(treeViewer.getElementsDroped(), treeViewer.getTargetToDrop());
				} catch(Exception e) {
					exceptionCatched(e);
				}
			}			
		};
		referenceToAction.setText("reference elements to");
		referenceToAction.setToolTipText("reference selected elements to");
		
		editAction = new Action() {
			public void run() {
				EntityRelationship selectedElement = treeViewer.getSelectedElement();
				if(selectedElement==null)
					MessageDialog.openError(null, "element not selected...", "select an element");
				else if(selectedElement.getHierarchicalPath().equals("/"))
					MessageDialog.openError(null, "element can't be updated...", "select a valid element");
				else {
					try {
						EntityEditorInput entityEditorInput = new EntityEditorInput(selectedElement);
						entityEditorInput.setAction(CrudAction.UPDATE);
						EntityEditor editor = (EntityEditor)ViewUtils.openOrBringToTopEditor(entityEditorInput, EntityEditor.ID, site);
						editor.setViewController(viewController);
					} catch(Exception e) {
						exceptionCatched(e);
					}															
				}
			}
		};
		editAction.setText("edit");
		editAction.setToolTipText("edit select element");
		editAction.setImageDescriptor(ImageProvider.getCommonImageDescriptor("edit.png"));
		deleteAction = new Action() {
			public void run() {
					EntityRelationship selectedElement = treeViewer.getSelectedElement();
					if(selectedElement==null)
						MessageDialog.openError(null, "elements not selected...", "select an element");
					else if(selectedElement.getHierarchicalPath().equals("/"))
						MessageDialog.openError(null, "element can't be deleted...", "select a valid element");					
					else {
						if(MessageDialog.openConfirm(null, "confirm...", "Are you want to delete the elements?")) {
							try {
								viewController.delete(treeViewer.getSelectedElements());
							} catch(Exception e) {
								exceptionCatched(e);
							}
						}
					}									
				}
			};
		deleteAction.setText("delete");
		deleteAction.setToolTipText("delete select element");
		deleteAction.setImageDescriptor(ImageProvider.getCommonImageDescriptor("delete.png"));
		
		refreshAction = new Action() {
			public void run() {
				try {
					List<EntityRelationship> elements = treeViewer.getSelectedElements();
					if(elements.size()>0)
						for(EntityRelationship i: elements) 
							treeViewer.refresh(i);
					else
						treeViewer.refresh(treeViewer.getRoot());
				} catch(Exception e) {
					exceptionCatched(e);
				}
			}
		};
		refreshAction.setText("refresh");
		refreshAction.setToolTipText("refresh selected elements");
		refreshAction.setImageDescriptor(ImageProvider.getCommonImageDescriptor("refresh.png"));
		
		previewAction = new Action() {
			public void run() {
				try {
					EntityRelationship entityRelationship = treeViewer.getSelectedElement();
					IStartClass preview;
					Map<String, Object> context = new HashMap<String, Object>();
					context.put("entityRelationship", entityRelationship);
					context.put("site", site);				
					preview = (IStartClass)Class.forName(entityRelationship.getEntity().getEntityType().getPreviewClass()).newInstance();
					preview.execute(context);
				} catch (Exception e) {
					exceptionCatched(e);
				}				
			}
		};
		previewAction.setText("preview");
		previewAction.setToolTipText("show a file preview");
		previewAction.setImageDescriptor(ImageProvider.getCommonImageDescriptor("preview.png"));
		
		processAction = new Action() {
			public void run() {
				try {			
					EntityRelationship entityRelationship = treeViewer.getSelectedElement();
					Map<String, Object> context = new HashMap<String, Object>();
					context.put("entityRelationship", entityRelationship);				
					IStartClass process;
					process = (IStartClass)Class.forName(entityRelationship.getEntity().getEntityType().getProcessClass()).newInstance();
					process.execute(context);
				} catch (Exception e) {
					exceptionCatched(e);
				}				
			}
		};
		processAction.setText("run process");
		processAction.setToolTipText("run a defined process");
		processAction.setImageDescriptor(ImageProvider.getCommonImageDescriptor("process.png"));
		
		editFileAction = new Action() {
			public void run() 
			{ 
				try {
					EntityRelationship entityRelationship = treeViewer.getSelectedElement();
					Map<String, Object> context = new HashMap<String, Object>();
					context.put("entityRelationship", entityRelationship);
					context.put("site", site);
					IStartClass process;
					process = (IStartClass)Class.forName(entityRelationship.getEntity().getEntityType().getEditfileClass()).newInstance();
					process.execute(context);
				} catch(Exception e) {
					exceptionCatched(e);
				}
			}
		};
		editFileAction.setText("edit file");
		editFileAction.setToolTipText("edit file in entity");
		editFileAction.setImageDescriptor(ImageProvider.getCommonImageDescriptor("editfile.png"));
	
		selectInViewAction= new Action() {
			public void run() {
				EntityRelationship relationship = treeViewer.getSelectedElement();
				selectInView(relationship);
			}
		};
		selectInViewAction.setText("select in view");
		selectInViewAction.setToolTipText("select in view");
		selectInViewAction.setImageDescriptor(ImageProvider.getCommonImageDescriptor("select.png"));		

		selectLinkInViewAction= new Action() {
			public void run() {
				EntityRelationship relationship = treeViewer.getSelectedElement();
				if(relationship.getLink()!=null)
					selectInView(relationship.getLink());
			}
		};
		selectLinkInViewAction.setText("select link in view");
		selectLinkInViewAction.setToolTipText("select link in view");
		selectLinkInViewAction.setImageDescriptor(ImageProvider.getCommonImageDescriptor("select.png"));				
		
		subentitiesEditAction = new Action() {
			public void run() {
				try {
					EntityRelationship entityRelationship = treeViewer.getSelectedElement();
					SubentitiesEditorInput subentitiesEditorInput = new SubentitiesEditorInput(entityRelationship);
					ViewUtils.openOrBringToTopEditor(subentitiesEditorInput, SubentitiesEditor.ID, site);
				} catch(Exception e) {
					exceptionCatched(e);
				}
			}
		};		
		subentitiesEditAction.setText("subentities editor");
		subentitiesEditAction.setToolTipText("edit for sub-entitites");
		subentitiesEditAction.setImageDescriptor(ImageProvider.getCommonImageDescriptor("editsubentities.png"));
		
		propertiesAction = new Action() {
			public void run() {
				EntityRelationship entityRelationship = treeViewer.getSelectedElement();
				PropertiesView propertiesView = (PropertiesView)site.getPage().findView(PropertiesView.ID);
				if(propertiesView==null) {
					try {
						propertiesView=(PropertiesView)site.getPage().showView(PropertiesView.ID);
					} catch (Exception e) {
						exceptionCatched(e);
					}
				}				
				propertiesView.setEntityRelationship(entityRelationship);
				propertiesView.refresh();
			}
		};
		propertiesAction.setText("properties");
		propertiesAction.setToolTipText("show entity properties");
		

		referencesToAction = new Action() {
			public void run() {
				EntityRelationship entityRelationship = treeViewer.getSelectedElement();
				ListEditorInput listEditorInput = new ListEditorInput(entityRelationship, new String[]{"description", "referenceStatus.referenceStatusId", "entity", "updatedBy", "changeDate"}, new String[]{"description", "reference status", "reference", "updated by", "change date"});
				listEditorInput.setID((long)1);
				listEditorInput.setLabels(new String[]{"description", "referenceStatus.code", "entity.code", "updatedBy", "changeDate"});
				ViewUtils.openOrBringToTopEditor(listEditorInput, ReferencesToEditor.ID, site);				
			}
		};
		referencesToAction.setText("references to");
		referencesToAction.setToolTipText("entity references to");
		referencesToAction.setImageDescriptor(ImageProvider.getCommonImageDescriptor("referencesTo.png"));

		
		referencesFromAction = new Action() {
			public void run() {
				EntityRelationship entityRelationship = treeViewer.getSelectedElement();
				ListEditorInput listEditorInput = new ListEditorInput(entityRelationship, new String[]{"description", "referenceStatus.referenceStatusId", "reference", "updatedBy", "changeDate"}, new String[]{"description", "reference status", "reference", "updated by", "change date"});
				listEditorInput.setLabels(new String[]{"description", "referenceStatus.code", "reference.code", "updatedBy", "changeDate"});
				ViewUtils.openOrBringToTopEditor(listEditorInput, ReferencesFromEditor.ID, site);				
			}
		};
		referencesFromAction.setText("references from");
		referencesFromAction.setToolTipText("entity references from");
		referencesFromAction.setImageDescriptor(ImageProvider.getCommonImageDescriptor("referencesFrom.png"));
		
		commentsAction = new Action() {
			public void run() {
				EntityRelationship entityRelationship = treeViewer.getSelectedElement();
				ListEditorInput listEditorInput = new ListEditorInput(entityRelationship, new String[]{"comment", "commentType.commentTypeId", "updatedBy", "changeDate"}, new String[]{"comment", "comment type", "updated by", "change date"});
				listEditorInput.setLabels(new String[]{"comment", "commentType.description", "updatedBy", "changeDate"});
				ViewUtils.openOrBringToTopEditor(listEditorInput, CommentsEditor.ID, site);				
			}
		};
		commentsAction.setText("comments");
		commentsAction.setToolTipText("entity comments");
		commentsAction.setImageDescriptor(ImageProvider.getCommonImageDescriptor("comments.png"));

		addCommentAction = new Action() {
			public void run() {
				final EntityRelationship entityRelationship = treeViewer.getSelectedElement();
				final EditablePopupDialog popupInfo = new EditablePopupDialog(site.getShell(), SWT.BORDER|SWT.SYSTEM_MODAL, true, true, true, true, true, "comments", "entity comments, press Esc key to exit");
				IAction acceptAction = new IAction() {
					@Override
					public boolean execute(Object object) {
						try {
							if(GenericValidator.isBlankOrNull(popupInfo.getComment()))
								throw new RuntimeException("comment can't be empty");
							EntityComment entityComment = new EntityComment();
							entityComment.setEntity(entityRelationship.getEntity());
							entityComment.setComment(popupInfo.getComment());
							entityComment.setCommentType(new CommentType());
							entityComment.getCommentType().setCommentTypeId(popupInfo.getType().getCommentTypeId());
							EngineContext.getDataContext().getEntityProviderFactory().getEntityCommentProvider().create(entityComment);							
							return true;
						} catch(Exception e) {
							exceptionCatched(e);
							return false;
						}
					}
				};
				popupInfo.setAcceptAction(acceptAction);
				popupInfo.setBlockOnOpen(true);
				popupInfo.open();	
			}
		};
		addCommentAction.setText("add comment");
		addCommentAction.setToolTipText("add entity comment");
		addCommentAction.setImageDescriptor(ImageProvider.getCommonImageDescriptor("comments.png"));

	}
	private void selectInView(EntityRelationship relationship) {
		if(relationship.getEntity().getEntityId()>0) {
			EntityCategory entityCategory = relationship.getEntity().getEntityType().getEntityCategory();
			EntityView entityView = (EntityView)site.getPage().findView(entityCategory.getIdView());
			if(entityView==null) {
				try {
					entityView = (EntityView)site.getPage().showView(entityCategory.getIdView());
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}		
			entityView.getTreeViewer().findAndSelectRelationship(relationship);
			site.getPage().bringToTop(entityView);
		}		
	}
	private void exceptionCatched(Exception e) {
		SingletonExceptionHandler.getInstance().showException(e);
	}	
}
